package com.lst.projectlib.component.CustomView;

import com.lst.projectlib.R;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CustomSpeechSearchView extends RelativeLayout implements TextWatcher , View.OnClickListener {
	private EditText mEtSearch = null;
	private TextView mIvLogo = null;
	private ImageView mIvDelete = null;
	private ICustomSearchListener mListener = null;
	
	public interface ICustomSearchListener {
		public void onTextChanged(String text);
	}

	public CustomSpeechSearchView(Context context) {
		super(context);
		initData(context);
	}
	
	public CustomSpeechSearchView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initData(context);
	}

	private void initData(Context context) {
		LayoutInflater.from(context).inflate(R.layout.view_speech_search, this);
		
		mEtSearch = (EditText) this.findViewById(R.id.search_edit);
		mEtSearch.addTextChangedListener(this);
		mIvLogo = (TextView) this.findViewById(R.id.search_logo_speech);
		
		mIvDelete = (ImageView) this.findViewById(R.id.search_delete);
		mIvDelete.setOnClickListener(this);
	}
	
	public void setCustomSearchListener(ICustomSearchListener listener) {
		mListener = listener;
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		String str = s.toString().trim();
		if (str != null && !str.equals("")){
			mIvLogo.setVisibility(View.GONE);
			mIvDelete.setVisibility(View.VISIBLE);
		}
		else {
			mIvLogo.setVisibility(View.VISIBLE);
			mIvDelete.setVisibility(View.GONE);
		}
		
		if (mListener != null) {
			mListener.onTextChanged(str);
		}
	}

	@Override
	public void afterTextChanged(Editable s) {
	}
	
	public void clearText() {
		mEtSearch.setText("");
	}
	
	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.search_delete){
			mEtSearch.setText("");
		}
	}

	public void setHintText(String text) {
		if (mIvLogo != null) {
			mIvLogo.setText(text);
		}
	}
}
